<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Product;
use pay\WxPay;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use think\facade\View;

class Order extends BaseController
{
    //订单详情
    public function orderDetail()
    {
        $get = Request::get();
        $list = \app\admin\model\Order::orderInfo($get['id']);
        $op = \app\admin\model\Order::suffix('_product')
            ->alias('op')
            ->field('op.*,p.price as pprice,p.price_shop,p.photo_x')
            ->where('order_id','=',$list['id'])
            ->join('product p','p.id=op.pid')
            ->select()->toArray();
        View::assign('op',$op);
        View::assign('list',$list);
        return View::fetch();
    }
    //订单列表
    public function orderList()
    {
        $where = [];
        $param = Request::param();
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[] = ['o.order_sn','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $where[] = ['os.status_id','in',$param['cid']];
        }
        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'sta'=>isset($param['status'])?$param['status']:[],
        ];
        View::assign($data);
        $status = \app\admin\model\Order::suffix('_status')->select()->toArray();
        View::assign('status',$status);
        $list = \app\admin\model\Order::orderList($where);
        //分页
        $page = $list->render();
        View::assign(['list'=>$list,'page'=>$page]);
        View::assign('list',$list);
        return View::fetch();
    }
    //订单删除
    public function orderDel()
    {
        $get = Request::get();
        \app\admin\model\Order::destroy($get['id']);
        $this->success('删除成功',url('order/orderList'));
    }
    //订单发货
    public function deliver()
    {
        $post = Request::post();
        $edit = [
            'id'=>$post['id'],
            'logistics'=>$post['dh'],
            'status'=>12
        ];
        try{
            $res = \app\admin\model\Order::update($edit);
        }catch (\Exception $e){
            $res = false;
        }
        return $res;
    }
    public function getNotice()
    {
        $xml = file_get_contents("php://input");//接受Xml包
        Log::record($xml);
    }
    //订单退款
    public function backMoney()
    {
        $post = Request::post();
        $order = \app\admin\model\Order::where('id','=',$post['id'])->find()->toArray();
        $url = "http://shop.fzyhh.top/index.php/admin/Order/getNotice";
        $pay = new WxPay($url);
        $rt = $pay->backMoney("{$order['order_sn']}","B{$order['order_sn']}",0.01,0.01);
//        $rt = $pay->backMoney("20210522191622","B20210522191622",0.01,0.01);
        if (isset($rt) && !empty($rt)){
            if ($rt['return_code']=="SUCCESS"){
                $edit = [
                    'id'=>$post['id'],
                    'out_refund_no'=>$rt['out_refund_no'],
                    'status'=>30
                ];
                \app\admin\model\Order::update($edit);
                return true;
            }else{
                return false;
            }
        }
    }
    //拒绝退款
    public function nobackMoney()
    {
        $post = Request::post();
        $order = \app\admin\model\Order::where('id','=',$post['id'])->find()->toArray();
        if (isset($order) && !empty($order)){
            $edit = [
                'id'=>$post['id'],
                'status'=>$order['history_status']
            ];
            try{
                $res = \app\admin\model\Order::update($edit);
            }catch (\Exception $e){
                $res = false;
            }
            return $res;

        }
    }





}